//
//  WPReturnCredentialsTableViewCell.m
//  wobingwoyi
//
//  Created by WGP on 2016/12/22.
//  Copyright © 2016年 WGP. All rights reserved.
//

#import "WPReturnCredentialsTableViewCell.h"
#import "WPMRModel.h"
#import "WPReturnListModel.h"
#import "WPCredentialsCollectionViewCell.h"

@interface WPReturnCredentialsTableViewCell ()<UICollectionViewDelegate,UICollectionViewDataSource>

@property(nonatomic,strong)UIView *baseInfoView;
@property(nonatomic,strong)UIView *ZDView;
@property(nonatomic,strong)UILabel *titleLabel;
@property(nonatomic,strong)UILabel *treamentStateBtn;
@property(nonatomic,strong)UIButton *ZXBtn;
@property(nonatomic,strong)UILabel *diseaseLabel;
@property(nonatomic,strong)UILabel *doctorLabel;
@property(nonatomic,strong)UILabel *hospitalLabel;
@property(nonatomic,strong)UILabel *diseaseTextField;
@property(nonatomic,strong)UILabel *doctorTextField;
@property(nonatomic,strong)UILabel *hospitalTextField;

@property(nonatomic,strong)UIView *containtView;
@property(nonatomic,strong)UIView *topView;
@property(nonatomic,strong)UILabel *timeLabel;
@property(nonatomic,strong)UILabel *specificTimeLabel;
@property(nonatomic,strong)UICollectionView *collectionView;
@property(nonatomic,strong)UILabel *descriptionLabel;

@property(nonatomic,strong)NSArray *urls;
@property(nonatomic,strong)NSArray *timesArr;

@end

@implementation WPReturnCredentialsTableViewCell

-(instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier])
    {
        self.selectionStyle  = UITableViewCellSelectionStyleNone;
        self.contentView.backgroundColor = GlobalBgColor;
        
        [self setupUI];
    }
    return self;
}

-(void)setupUI
{
    [self baseInfoView];
    [self setupLabels];
    [self containtView];
    [self topView];
    [self collectionView];
    [self descriptionLabel];
}

-(UIView *)baseInfoView
{
    if (_baseInfoView == nil)
    {
        UIImage *ZDImage = [UIImage imageNamed:@"zhenduan"];
        
        _baseInfoView = [UIView new];
        _baseInfoView.backgroundColor = [UIColor whiteColor];
        [self.contentView addSubview:_baseInfoView];
        [_baseInfoView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.right.top.equalTo(self.contentView);
            make.height.mas_equalTo(176);
        }];
        
        self.titleLabel = [UILabel new];
        self.titleLabel.text = @"二诊诊断凭据";
        self.titleLabel.font = [UIFont systemFontOfSize:15];
        self.titleLabel.textColor = GLobalFontColor;
        self.titleLabel.textAlignment = NSTextAlignmentCenter;
        [_baseInfoView addSubview:self.titleLabel];
        [self.titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.top.right.equalTo(_baseInfoView);
            make.height.mas_equalTo(44);
        }];
        
        self.treamentStateBtn = [UILabel new];
        self.treamentStateBtn.text = @"治疗状态";
        self.treamentStateBtn.textColor = RGB_COLOR(208, 208, 213);
        [_baseInfoView addSubview:self.treamentStateBtn];
        self.treamentStateBtn.font = [UIFont systemFontOfSize:14];
        CGSize size = [self.treamentStateBtn.text sizeWithMaxSize:CGSizeMake(CGFLOAT_MAX, CGFLOAT_MAX) font:14];
        [self.treamentStateBtn mas_makeConstraints:^(MASConstraintMaker *make) {
            make.width.mas_equalTo(size.width+1);
            make.right.equalTo(_baseInfoView).offset(-8);
            make.top.equalTo(self.titleLabel.mas_bottom);
            make.height.mas_equalTo(44);
        }];
        
        UIImageView *imageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"treamentState"]];
        [_baseInfoView addSubview:imageView];
        
        [imageView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.centerY.equalTo(self.treamentStateBtn);
            make.right.equalTo(self.treamentStateBtn.mas_left).offset(-8);
            make.width.mas_equalTo(15);
            make.height.mas_equalTo(20);
        }];
        
        UIImageView *lineView = [[UIImageView alloc] init];
        lineView.backgroundColor = GLobalLineColor;
        [_baseInfoView addSubview:lineView];
        
        [lineView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.width.mas_equalTo(0.5);
            make.height.mas_equalTo(24);
            make.centerY.equalTo(imageView);
            make.right.equalTo(imageView.mas_left).offset(-10);
        }];
        
        self.ZXBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        [self.ZXBtn setBackgroundImage:[UIImage imageNamed:@"ZX_normal"] forState:UIControlStateNormal];
        [self.ZXBtn setBackgroundImage:[UIImage imageNamed:@"ZX_highlight"] forState:UIControlStateHighlighted];
        [self.ZXBtn setTitle:@"西医" forState:UIControlStateNormal];
        [self.ZXBtn setTitleColor:RGB_SAME(102) forState:UIControlStateNormal];
        self.ZXBtn .titleLabel.font = [UIFont systemFontOfSize:12];
        [_baseInfoView addSubview:self.ZXBtn];
        
        UIImage *image = [UIImage imageNamed:@"ZX_normal"];
        [self.ZXBtn mas_makeConstraints:^(MASConstraintMaker *make) {
            make.centerY.equalTo(self.treamentStateBtn);
            make.right.equalTo(lineView).offset(-10);
            make.width.mas_equalTo(image.size.width);
        }];
        
        UIView *ZDView = [UIView new];
        self.ZDView = ZDView;
        ZDView.backgroundColor = GlobalBgColor;
        [self.contentView addSubview:ZDView];
        [ZDView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.right.equalTo(_baseInfoView);
            make.top.equalTo(_baseInfoView.mas_bottom);
            make.height.mas_equalTo(ZDImage.size.height+30);
        }];
        UIImageView *ZDImageView = [[UIImageView alloc] initWithImage:ZDImage];
        [ZDView addSubview:ZDImageView];
        [ZDImageView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(_baseInfoView).offset(10);
            make.centerY.equalTo(ZDView);
        }];
        UILabel *ZDLabel = [UILabel new];
        ZDLabel.textColor = GLobalFontColor;
        ZDLabel.font = [UIFont systemFontOfSize:15];
        ZDLabel.text = @"诊断单";
        [ZDView addSubview:ZDLabel];
        [ZDLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.centerY.equalTo(ZDView);
            make.left.equalTo(ZDImageView.mas_right).offset(10);
        }];
        
        UIImageView *lineView1 = [UIImageView lineImageView];
        UIImageView *lineView2 = [UIImageView lineImageView];
        UIImageView *lineView3 = [UIImageView lineImageView];
        UIImageView *lineView4 = [UIImageView lineImageView];
        [_baseInfoView addSubview:lineView1];
        [_baseInfoView addSubview:lineView2];
        [_baseInfoView addSubview:lineView3];
        [_baseInfoView addSubview:lineView4];
        lineView1.frame = CGRectMake(0, 0, SCREEN_WIDTH, 0.5);
        lineView2.frame = CGRectMake(0, 44, SCREEN_WIDTH, 0.5);
        lineView3.frame = CGRectMake(10, 88, SCREEN_WIDTH-10, 0.5);
        lineView4.frame = CGRectMake(10, 132, SCREEN_WIDTH-10, 0.5);
    }
    return _baseInfoView;
}

-(void)setupLabels
{
    UILabel *firstLabel = [self labelWith:@"疾病名称:" textColor:GLobalFontColor];
    self.diseaseLabel = firstLabel;
    UILabel *secondLabel = [self labelWith:@"诊治医生:" textColor:GLobalFontColor];
    self.doctorLabel = secondLabel;
    UILabel *fourthLabel = [self labelWith:@"治疗医院:" textColor:GLobalFontColor];
    self.hospitalLabel = fourthLabel;
    
    
    [_baseInfoView addSubview:firstLabel];
    [_baseInfoView addSubview:secondLabel];
    [_baseInfoView addSubview:fourthLabel];
    
    CGSize size = [firstLabel.text sizeWithMaxSize:CGSizeMake(CGFLOAT_MAX, CGFLOAT_MAX) font:14];
    CGFloat margin = (44-size.height)/2.0;
    
    [firstLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(_baseInfoView).offset(10);
        make.width.mas_equalTo(size.width+1);
        make.top.equalTo(_baseInfoView).offset(44 + margin);
    }];
    
    [secondLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.width.equalTo(firstLabel);
        make.top.equalTo(firstLabel.mas_bottom).offset(2*margin);
    }];
    
    [fourthLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.width.equalTo(firstLabel);
        make.top.equalTo(secondLabel.mas_bottom).offset(2*margin);
    }];
    
    
    self.diseaseTextField = [self labelWith:@"请输入疾病名称" textColor:RGB_COLOR(208, 208, 213)];
    self.doctorTextField = [self labelWith:@"请输入诊治医生" textColor:RGB_COLOR(208, 208, 213)];
    self.hospitalTextField = [self labelWith:@"请输入治疗医院" textColor:RGB_COLOR(208, 208, 213)];
    [_baseInfoView addSubview:self.diseaseTextField];
    [_baseInfoView addSubview:self.doctorTextField];
    [_baseInfoView addSubview:self.hospitalTextField];
    
    [self.diseaseTextField mas_makeConstraints:^(MASConstraintMaker *make) {
        make.height.mas_equalTo(42);
        make.centerY.equalTo(self.diseaseLabel);
        make.right.equalTo(self.ZXBtn.mas_left).offset(5);
        make.left.equalTo(self.diseaseLabel.mas_right).offset(8);
    }];
    
    [self.doctorTextField mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.height.equalTo(self.diseaseTextField);
        make.right.equalTo(_baseInfoView).offset(-10);
        make.centerY.equalTo(self.doctorLabel);
    }];
    
    [self.hospitalTextField mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.height.equalTo(self.diseaseTextField);
        make.right.equalTo(_baseInfoView).offset(-10);
        make.centerY.equalTo(self.hospitalLabel);
    }];
}

-(UIView *)containtView
{
    if (_containtView == nil)
    {
        _containtView = [UIView new];
        _containtView.backgroundColor = [UIColor whiteColor];
        [self.contentView addSubview:_containtView];
        
        [_containtView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.right.equalTo(self.contentView);
            make.top.equalTo(self.ZDView.mas_bottom);
            make.bottom.equalTo(self.contentView).offset(-10);
        }];
    }
    return _containtView;
}

-(UIView *)topView
{
    if (_topView == nil)
    {
        _topView = [[UIView alloc] init];
        [self.containtView addSubview:_topView];
        
        [_topView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.top.right.equalTo(self.containtView);
            make.height.mas_equalTo(44);
        }];
        
        UIImageView *topLine = [UIImageView lineImageView];
        UIImageView *bottomLine = [UIImageView lineImageView];
        
        [_topView addSubview:topLine];
        [_topView addSubview:bottomLine];
        
        [topLine mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.right.top.equalTo(_topView);
            make.height.mas_equalTo(0.5);
        }];
        
        [bottomLine mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.right.bottom.equalTo(_topView);
            make.height.mas_equalTo(0.5);
        }];
        
        _timeLabel = [UILabel new];
        _timeLabel.textColor = GLobalFontColor;
        _timeLabel.font = [UIFont systemFontOfSize:15];
        _timeLabel.text = @"二诊时间:";
        [_topView addSubview:_timeLabel];
        
        [_timeLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(_topView).offset(10);
            make.centerY.equalTo(_topView);
        }];
        
        _specificTimeLabel = [UILabel new];
        _specificTimeLabel.font = [UIFont systemFontOfSize:15];
        _specificTimeLabel.textColor = GLobalFontColor;
        _specificTimeLabel.text = @"二诊时间:";
        [_topView addSubview:_specificTimeLabel];
        
        [_specificTimeLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.centerY.equalTo(_topView);
            make.left.equalTo(_timeLabel.mas_right).offset(8);
        }];
        
    }
    return _topView;
}

-(UICollectionView *)collectionView
{
    if (_collectionView == nil)
    {
        UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
        _collectionView = [[UICollectionView alloc] initWithFrame:CGRectZero collectionViewLayout:layout];
        _collectionView.delegate = self;
        _collectionView.dataSource = self;
        _collectionView.allowsSelection = NO;
        _collectionView.showsHorizontalScrollIndicator = NO;
        
        CGFloat itemWidth = (SCREEN_WIDTH - 4*10)/3.0;
        CGFloat itemHeight = itemWidth*90/112.5;
        layout.itemSize = CGSizeMake(itemWidth, itemHeight);
        layout.minimumLineSpacing = 0;
        layout.minimumInteritemSpacing = 0;
        
        [self.containtView addSubview:_collectionView];
        [_collectionView registerClass:[WPCredentialsCollectionViewCell class] forCellWithReuseIdentifier:@"WPCredentialsCollectionViewCell"];
        _collectionView.backgroundColor = [UIColor whiteColor];
        
        
        [_collectionView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.right.equalTo(self.contentView).offset(-10);
            make.left.equalTo(self.contentView).offset(10);
            make.top.equalTo(self.topView.mas_bottom).offset(10);
            make.height.mas_equalTo(itemHeight+2);
        }];
        
        UITapGestureRecognizer *tgr = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapGestureRecongnizer:)];
        [_collectionView addGestureRecognizer:tgr];
    }
    return _collectionView;
}

-(UILabel *)descriptionLabel
{
    if (_descriptionLabel == nil)
    {
        _descriptionLabel = [UILabel new];
        _descriptionLabel.textColor = GLobalFontColor;
        _descriptionLabel.font = [UIFont systemFontOfSize:15];
        _descriptionLabel.numberOfLines = 0;
        [self.containtView addSubview:_descriptionLabel];
        
        [_descriptionLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(self.collectionView.mas_bottom).offset(10);
            make.left.equalTo(self.contentView).offset(10);
            make.right.equalTo(self.contentView).offset(-10);
            make.bottom.equalTo(self.containtView).offset(-15);
        }];
    }
    return _descriptionLabel;
}

-(void)setModel:(WPMRModel *)model
{
    _model = model;
    
    if (self.index == 0)
    {
        
        self.descriptionLabel.text = model.firstOptionStr.length > 0 ? model.firstOptionStr : @"暂无病例描述";
        
        self.descriptionLabel.textColor = model.firstOptionStr.length > 0 ? GLobalFontColor : RGB_SAME(153);
        self.urls = model.firstOptionUrls;
        
        self.specificTimeLabel.text = model.treatmentTime;
        
        [self.collectionView reloadData];
    }
    else
    {
        
        WPReturnListModel *returnListModel = model.returnVisit_list[self.index-1];
        
        self.doctorTextField.text = returnListModel.doctor;
        self.diseaseTextField.text = returnListModel.name;
        self.hospitalTextField.text = returnListModel.hospital;
        self.treamentStateBtn.text = returnListModel.cureStatus;
        [self.ZXBtn setTitle:returnListModel.type forState:UIControlStateNormal];
        
        self.doctorTextField.textColor = GLobalFontColor;
        self.diseaseTextField.textColor = GLobalFontColor;
        self.hospitalTextField.textColor = GLobalFontColor;
        self.treamentStateBtn.textColor = GLobalFontColor;
        
        self.specificTimeLabel.text = returnListModel.treatmentTime;
        
        self.descriptionLabel.text = returnListModel.descpStr.length > 0 ? returnListModel.descpStr : @"暂无病例描述";
        self.descriptionLabel.textColor = returnListModel.descpStr.length > 0 ? GLobalFontColor : RGB_SAME(153);
        self.urls = returnListModel.descpURLS;
        
        [self.collectionView reloadData];
    }
    
    for(NSString *url in self.urls)
    {
        //异步下载图片
        [[SDWebImageManager sharedManager] downloadImageWithURL:[NSURL URLWithString:url] options:0 progress:^(NSInteger receivedSize, NSInteger expectedSize) {
            
            
        } completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType, BOOL finished, NSURL *imageURL) {
            
        }];
    }
    
}

-(void)setIndex:(NSInteger)index
{
    _index = index;
    
    self.titleLabel.text = [NSString stringWithFormat:@"%@诊诊断凭据",self.timesArr[index]];
    self.timeLabel.text = [NSString stringWithFormat:@"%@诊时间:",self.timesArr[index]];
}

-(void)tapGestureRecongnizer:(UITapGestureRecognizer *)tgr
{
    if ([self.delegate respondsToSelector:@selector(WPCredentialsTableViewCellAddPicture:index:)])
    {
        [self.delegate WPCredentialsTableViewCellAddPicture:nil index:self.index];
    }
}

#pragma mark - collectionView的数据源方法
-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    if (self.urls.count >3)
    {
        return 3;
    }
    else if(self.urls.count > 0)
    {
        return self.urls.count;
    }
    else
    {
        return 1;
    }
}

-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    WPCredentialsCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"WPCredentialsCollectionViewCell" forIndexPath:indexPath];
    if (self.urls.count == 0)
    {
        cell.url = nil;
    }
    else
    {
        cell.url = self.urls[indexPath.item];
    }
    
    return cell;
}

-(UILabel *)labelWith:(NSString *)title textColor:(UIColor *)textColor
{
    UILabel *label = [UILabel new];
    label.text = title;
    label.textColor = textColor;
    label.font = [UIFont systemFontOfSize:14];
    
    return label;
}

-(NSArray *)timesArr
{
    if (_timesArr == nil)
    {
        _timesArr = [NSArray arrayWithObjects:@"一",@"二",@"三",@"四",@"五",@"六",@"七",@"八",@"九",@"十",@"十一",@"十二", nil];
    }
    return _timesArr;
}
@end
